package com.snoob.springboot.bffServiceAdmin.controller;

import com.snoob.springboot.base.BaseException;
import com.snoob.springboot.base.BaseLoginUserInfo;
import com.snoob.springboot.base.BasePageResult;
import com.snoob.springboot.base.BaseResult;
import com.snoob.springboot.enterprise.controller.EnterpriseController;
import com.snoob.springboot.enterprise.controller.EnterpriseRegisterController;
import com.snoob.springboot.enterprise.controller.EnterpriseUserController;
import com.snoob.springboot.enterprise.entity.EnterpriseEntity;
import com.snoob.springboot.enterprise.entity.EnterpriseUserEntity;
import com.snoob.springboot.enums.SysStateEnum;
import com.snoob.springboot.pipleline.dto.*;
import com.snoob.springboot.pipleline.qo.SelectEnterpriseQo;
import com.snoob.springboot.pipleline.qo.SelectEnterpriseUserQo;
import com.snoob.springboot.pipleline.vo.CreateWorkOrderVoReq;
import com.snoob.springboot.pipleline.vo.EnterpriseRegisterVoReq;
import com.snoob.springboot.pipleline.vo.SelectEnterpriseUserByPageVoRes;
import com.snoob.springboot.util.ConvertUtils;
import com.snoob.springboot.wConfig.Intercept.auth.Auth;
import com.snoob.springboot.wConfig.Intercept.auth.AuthInfo;
import com.snoob.springboot.workorder.controller.WorkOrderController;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @描述：企业
 * @作者：lixing_java@163.com
 * @时间：2022/5/14 16:16
 */
@RestController
@RequestMapping("enterprise")
public class EnterpriseBffController {
    @Resource
    private EnterpriseRegisterController enterpriseRegisterFeign;

    @Resource
    private EnterpriseController enterpriseFeign;

    @Resource
    private EnterpriseUserController enterpriseUserFeign;

    @Resource
    private WorkOrderController workOrderFeign;

    /**
     * @描述 企业注册（第一步：创建企业；第二步：创建用户）
     * @接口地址 127.0.0.1:8801/bff/enterprise/enterpriseRegister
     */
    @PostMapping("enterpriseRegister")
    @Transactional
    public BaseResult<EnterpriseRegisterDtoRes> enterpriseRegister(@RequestBody EnterpriseRegisterDtoReq dto) {
        EnterpriseRegisterVoReq enterpriseRegisterVoReq = ConvertUtils.convert(dto, EnterpriseRegisterVoReq.class);
        EnterpriseRegisterDtoRes enterpriseRegisterDtoRes = ConvertUtils.convert(
                enterpriseRegisterFeign.enterpriseRegister(enterpriseRegisterVoReq), EnterpriseRegisterDtoRes.class);
        return new BaseResult<EnterpriseRegisterDtoRes>().success(enterpriseRegisterDtoRes);
    }

    /**
     * @描述 分页查询企业用户（关联企业表）信息
     * @接口地址 127.0.0.1:8801/bff/enterprise/selectEnterpriseUserByPage
     */
    @PostMapping("selectEnterpriseUserByPage")
    @Auth
    public BasePageResult<SelectEnterpriseUserByPageVoRes> selectEnterpriseUserByPage(
            @AuthInfo BaseLoginUserInfo baseLoginUserInfo,
            @RequestBody SelectEnterpriseUserQo qo) {
        return new BasePageResult<>(enterpriseUserFeign.selectEnterpriseUserByPage(qo));
    }

    /**
     * 创建企业工单（冗余企业信息、企业用户信息到工单模块的相关子表中【定时器同步信息到子表】）（不同服务下）
     * @接口地址 127.0.0.1:8801/bff/enterprise/workOrderCreate
     */
    @PostMapping("createWorkOrder")
    @Transactional
    @Auth
    public String createWorkOrder(
            @AuthInfo BaseLoginUserInfo baseLoginUserInfo,
            @RequestBody CreateWorkOrderDtoReq dto) {
        // 查询用户信息
        SelectEnterpriseUserQo selectEnterpriseUserQo = new SelectEnterpriseUserQo();
        selectEnterpriseUserQo.setUserAccount(dto.getUserAccount());
        EnterpriseUserEntity enterpriseUserEntity = enterpriseUserFeign.selectEnterpriseUser(selectEnterpriseUserQo);
        if (null == enterpriseUserEntity) {
            throw new BaseException("用户不存在");
        }
        if (SysStateEnum.STATE_NO.getValue() == enterpriseUserEntity.getShowState()) {
            throw new BaseException("用户不可用");
        }

        // 查询企业信息
        SelectEnterpriseQo selectEnterpriseQo = new SelectEnterpriseQo();
        selectEnterpriseQo.setEnterpriseCode(enterpriseUserEntity.getEnterpriseCode());
        EnterpriseEntity enterpriseEntity = enterpriseFeign.selectEnterprise(selectEnterpriseQo);
        if (null == enterpriseEntity) {
            throw new BaseException("企业不存在");
        }
        if (SysStateEnum.STATE_NO.getValue() == enterpriseEntity.getShowState()) {
            throw new BaseException("企业不可用");
        }

        // 封装请求参数
        CreateWorkOrderVoReq CreateWorkOrderVoReq = new CreateWorkOrderVoReq();
        CreateWorkOrderVoReq.setOrderName(dto.getOrderName());
        CreateWorkOrderVoReq.setEnterpriseName(enterpriseEntity.getEnterpriseName());
        CreateWorkOrderVoReq.setEnterpriseCode(enterpriseEntity.getEnterpriseCode());
        CreateWorkOrderVoReq.setEnterpriseShowState(enterpriseEntity.getShowState());
        CreateWorkOrderVoReq.setEnterpriseSeq(enterpriseEntity.getSeq());
        CreateWorkOrderVoReq.setUserAccount(enterpriseUserEntity.getUserAccount());
        CreateWorkOrderVoReq.setUserName(enterpriseUserEntity.getUserName());
        CreateWorkOrderVoReq.setUserPhone(enterpriseUserEntity.getUserPhone());
        CreateWorkOrderVoReq.setUserShowState(enterpriseUserEntity.getShowState());
        CreateWorkOrderVoReq.setUserSeq(enterpriseUserEntity.getSeq());
        return workOrderFeign.workOrderCreate(CreateWorkOrderVoReq);
    }

}
